<template>
  <layout title="补贴信息汇总浏览表">
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        mode="search"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          @click="search()"
        >搜索</a-button>
        <!-- <a-button
          class="action-button"
          type="primary"
          @click="handlerTableAdd"
        >新增农户</a-button> -->
      </custom-form>
    </section>

    <!-- 产品列表 -->
    <a-table
      :dataSource="tableData"
      :columns="tableColumns"
      :rowKey="record => record.s_a_id"
      :pagination="false"
      :loading="tableLoading"
      :customRow="customTableRow"
      @change="handleTableChange"
      :scroll="{
        y: tableClientHeight + 100,
        x: '100%',
      }"
      bordered
    >
      <template
        slot="used_percent"
        slot-scope="row"
      >
        {{ (!row.member_total ? 0 : (+row.used / +row.member_total) * 100).toFixed(2) }}%
      </template>
      <template
        slot="un_used"
        slot-scope="row"
      >
        {{ +row.member_total - +row.used }}
      </template>
      <template
        slot="un_used_percent"
        slot-scope="row"
      >
        {{ (!row.member_total ? 0 : ((+row.member_total - +row.used) / +row.member_total) * 100).toFixed(2) }}%
      </template>

      <!-- 补贴总金额（元） -->
      <template
        slot="pay_price_total"
        slot-scope="row"
      >
        {{ ((+row.pay_unit === 1 ? (row.plant_used || 0)  : (row.field_used || 0)) * row.pay_price).toFixed(2) }}
        
      </template>

      <template
        slot="bt_size"
        slot-scope="row"
      >
        {{(+row.pay_unit === 1 ? (row.plant_used || 0)  : (row.field_used || 0))}}
      </template>

      <template
        slot="plant"
        slot-scope="row"
      >
        {{(+row.plant || 0).toFixed(3)}}
      </template>

      <template
        slot="pay_unit"
        slot-scope="row"
      >
        {{getLabel(row.pay_unit, [], 'pay_unit')}}
      </template>

      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row"
      >
        <a-button
          class="action-button"
          type="primary"
          size="small"
          @click="handleTableEdit(row)"
        >编辑</a-button>
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button
            class="action-button"
            type="danger"
            size="small"
          >删除</a-button>
        </a-popconfirm>
        <a-button
          class="action-button"
          type="primary"
          size="small"
          @click="handlerFamily(row)"
        >管理家庭成员</a-button>
        <!-- <a-button
          class="action-button"
          type="primary"
          size="small"
          @click="handleTableEdit(row)"
        >项目管理</a-button> -->
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <!-- <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :show-total="showTotal"
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination> -->

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      :footer="null"
      centered
      @cancel="visible = false"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        v-bind="formItemLayout"
        layout="horizontal"
      ></custom-form>
      <!-- <section slot="footer"> -->
      <a-button
        type="primary"
        @click="handleSubmit"
      >提交</a-button>
      <!-- </section> -->
    </a-modal>

  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { subsidyServices, IStatistics } from '@/common/services/subsidy'
import { PayUnitType } from '@/common/enum'
import { OrderEnum } from '@/mixin/table'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  @Prop({
    type: String
  })
  public currentAreaId!: string

  public commonParams = {
    s_a_id: ''
  }

  // 产品列表搜索条件
  public tableSearch = {
    is_used: ''
  }

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '加入补贴状态',
      dataIndex: 'is_used',
      key: 'is_used',
      width: 140,
      form: {
        type: 'a-radio-group',
        placeholder: '请选择补贴状态',
        isShowList: false,
        isShowQuery: true,
        dict: [
          {
            label: '已加入',
            value: '1'
          },
          {
            label: '未加入',
            value: '2'
          }
        ]
      }
    },
    {
      title: '行政编码',
      dataIndex: 's_a_id',
      key: 's_a_id',
      width: 130,
      form: {
        type: 'a-input',
        placeholder: '编号自动生成',
        disabled: true
      }
    },
    {
      title: '行政区',
      dataIndex: 's_a_nam',
      key: 's_a_nam',
      width: 140,
      form: {
        type: 'a-input',
        placeholder: '请输入行政区名称'
      }
    },
    {
      title: '农户总数',
      dataIndex: 'member_total',
      key: 'member_total',
      align: 'right',
      width: 100,
      sorter: true,
      sortDirections: ['ascend', 'descend'],
      form: {
        type: 'a-textarea',
        placeholder: '请输入家庭住址'
      }
    },
    {
      title: '享受补贴',
      children: [
        {
          title: '总户数',
          dataIndex: 'used',
          key: 'used',
          sorter: true,
          sortDirections: ['ascend', 'descend'],
          align: 'right',
          width: 80,
        },
        {
          title: '占比%',
          scopedSlots: { customRender: 'used_percent' },
          key: 'used_percent',
          align: 'right',
          width: 80,
        },
      ]
    },
    
    {
      title: '未享受补贴',
      children: [
        {
          title: '总户数',
          width: 80,
          align: 'right',
          // sorter: true,
          // sortDirections: ['ascend', 'descend'],
          scopedSlots: { customRender: 'un_used' },
          key: 'un_used'
        },
        {
          title: '占比%',
          width: 80,
          align: 'right',
          scopedSlots: { customRender: 'un_used_percent' },
          key: 'un_used_percent'
        },
      ]
    },
    
    {
      title: '确权面积（亩）',
      children: [
        {
          title: '小计',
          dataIndex: 'field',
          key: 'field',
          align: 'right',
          width: 110,
          sorter: true,
          sortDirections: ['ascend', 'descend'],
          form: {
            type: 'a-input',
            placeholder: '请输入耕地总面积',
            renderMode: ['farmland']
          }
        },
        {
          title: '承包地',
          dataIndex: 'field1',
          key: 'field1',
          width: 110,
          align: 'right',
          sorter: true,
          sortDirections: ['ascend', 'descend'],
          form: {
            type: 'a-input',
            placeholder: '请输入耕地总面积',
            renderMode: ['farmland']
          }
        },
        {
          title: '增加面积',
          dataIndex: 'field2',
          align: 'right',
          key: 'field2',
          width: 110,
          sorter: true,
          sortDirections: ['ascend', 'descend'],
          form: {
            type: 'a-input',
            placeholder: '请输入耕地总面积',
            renderMode: ['farmland']
          }
        },
        {
          title: '减少面积',
          dataIndex: 'field3',
          align: 'right',
          key: 'field3',
          width: 110,
          sorter: true,
          sortDirections: ['ascend', 'descend'],
          form: {
            type: 'a-input',
            placeholder: '请输入耕地总面积',
            renderMode: ['farmland']
          }
        },
      ]
    },

    // {
    //   title: '耕地总面积（亩）',
    //   width: 140,
    //   dataIndex: 'field',
    //   align: 'right',
    //   key: 'field'
    // },
    // {
    //   title: '承包地（亩）',
    //   width: 110,
    //   dataIndex: 'field1',
    //   align: 'right',
    //   key: 'field1'
    // },
    // {
    //   title: '增加耕地（亩）',
    //   width: 110,
    //   dataIndex: 'field2',
    //   align: 'right',
    //   key: 'field2'
    // },
    // {
    //   title: '减少耕地（亩）',
    //   width: 110,
    //   dataIndex: 'field3',
    //   align: 'right',
    //   key: 'field3'
    // },
    {
      title: '粮食种植面积（亩）',
      width: 130,
      align: 'right',
      sorter: true,
      sortDirections: ['ascend', 'descend'],
      scopedSlots: { customRender: 'plant' },
      key: 'plant'
    },
    {
      title: '每亩补贴金额',
      width: 130,
      dataIndex: 'pay_price',
      align: 'right',
      key: 'pay_price'
    },
    {
      title: '补贴面积',
      width: 110,
      dataIndex: 'pay_size_total',
      align: 'right',
      sorter: true,
      sortDirections: ['ascend', 'descend'],
      // scopedSlots: { customRender: 'bt_size' },
      key: 'pay_size_total'
    },
    // {
    //   title: '参与补贴的确权面积',
    //   width: 110,
    //   dataIndex: 'field_used',
    //   align: 'right',
    //   sorter: true,
    //   sortDirections: ['ascend', 'descend'],
    //   key: 'field_used'
    // },
    {
      title: '补贴参照',
      width: 110,
      key: 'pay_unit',
      align: 'center',
      scopedSlots: { customRender: 'pay_unit' },
      form: {
        type: 'a-select',
        placeholder: '请选择补贴核算依据',
        isShowFrom: false,
        dict: PayUnitType
      }
    },
    {
      title: '补贴总金额',
      key: 'pay_price_total',
      dataIndex: 'pay_price_total',
      sorter: true,
      sortDirections: ['ascend', 'descend'],
      // scopedSlots: { customRender: 'pay_price_total' },
      align: 'right',
      width: 120
    },
  ]

  public tableApi = {
    list: subsidyServices.getSubsidyList
    // add: famerServices.updateMemberGrain,
    // edit: famerServices.updateMemberGrain
    // del: userServices.deleteAdmin,
    // imageUpload: 'uploadStoreImage',
    // imageRemove: 'delStoreImage'
  }

  @Watch('currentAreaId', { immediate: true })
  public handlerCurrentAreaIdChange() {
    this.commonParams.s_a_id = this.currentAreaId
    this.getData()
  }

  public mounted() {}

  public async getData() {
    const res = await this.getTableList()
  }

  public handlerTableAdd() {
    this.tableAdd()
    this.$nextTick(() => {
      this.currentRow = Object.assign(
        {},
        { s_a_id: this.currentAreaId, role: 1 }
      )
    })
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: any) {
    this.tableEdit(Object.assign({}, row))
  }
}
</script>

<style scoped lang="less">
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
/deep/ .ant-table {
  th[key="member_total"], th[key="plant"], th[key^="pay"], th[key="pay_price_total"] {
    text-align: center !important;;
  }
}
// /deep/ .ant-table-thead th {
//   text-align: center!important;
// }
</style>
